package com.ego.order.controller;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

import com.ego.common.exception.GlobalException;
import com.ego.common.vo.BaseSearchVo;
import com.ego.common.vo.PageVo;
import com.ego.common.vo.order.OrderSubmitVo;
import com.ego.common.to.order.OrderDetailTo;
import com.ego.common.vo.order.PayAsyncVo;
import com.ego.order.vo.PayVo;
import com.ego.order.vo.WebParamVo;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.ego.order.po.Order;
import com.ego.order.service.OrderService;
import com.ego.common.util.Result;

import javax.validation.Valid;

/**
 * @author KamTang
 * @program: ego
 * @description 订单控制器
 * @date 2022-11-13 11:25:07
 */
@RestController
@RequestMapping("order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 搜索
     */
    @PostMapping("/search")
    public Result search(@Valid @RequestBody BaseSearchVo search) {
        PageVo<Order> page = orderService.search(search);
        return Result.ok().put("page", page);
    }

    /**
     * 获取近一周订单量（成功订单：待发货、已发货、已完成）
     */
    @GetMapping("/getWeeklyData")
    public Result getWeeklyData() {
        Map<String, List<?>> res = orderService.getWeeklyData();
        return Result.ok().put("res", res);
    }

    /**
     * 获取商品季度收益
     */
    @GetMapping("/getQuarterlyEarning")
    public Result getQuarterlyEarning() {
        Map<String, List<?>> res = orderService.getQuarterlyEarning();
        return Result.ok().put("res", res);
    }

    /**
     * 根据据订单号获取订单详情
     */
    @GetMapping("/detail/{orderSn}")
    public Result detail(@PathVariable("orderSn") String orderSn) {
        OrderDetailTo detail = orderService.detail(orderSn);
        return Result.ok().setData(detail);
    }

    /**
     * 分页获取会员订单信息
     */
    @PostMapping("/my-order-item")
    public Result page(@Valid @RequestBody WebParamVo param) {
        PageVo<Order> orders = orderService.pageWithItem(param);
        return Result.ok().put("page", orders);
    }

    /**
     * 根据订单号获取订单支付信息
     */
    @GetMapping("/pay-info/{orderSn}")
    public Result getPayInfo(@PathVariable("orderSn") String orderSn) {
        PayVo payVo = orderService.getPayInfo(orderSn);
        return Result.ok().put("payVo", payVo);
    }

    /**
     * 提交订单
     */
    @PostMapping("/submit")
    public Result submit(@Valid @RequestBody OrderSubmitVo submitVo) {
        try {
            OrderDetailTo order = orderService.submit(submitVo);
            if (ObjectUtils.isNotEmpty(order)) {
                return Result.ok().put("orderCreateTo", order);
            } else {
                return Result.error();
            }
        } catch (GlobalException e) {
            return Result.error(e.getCode(), e.getMsg());
        }
    }

    /**
     * 处理支付结果
     */
    @PostMapping("/handle-pay-result")
    public Result handlePayResult(@Valid @RequestBody PayAsyncVo vo) throws ExecutionException, InterruptedException {
        if (orderService.handlePayResult(vo)) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    /**
     * 获取订单状态
     */
    @GetMapping("/status/{orderSn}")
    public Result get(@PathVariable("orderSn") String orderSn) {
        Order order = orderService.get(orderSn);
        return Result.ok().setData(order);
    }

}
